package com.ssps.front.ternant.service.impl;

import com.github.pagehelper.PageHelper;
import com.ssps.entity.*;
import com.ssps.front.ternant.service.TernantService;
import com.ssps.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Hala Zhou
 * @time 2020/07/21 22:20:31
 */
@Service
@Transactional
public class TernantServiceImpl implements TernantService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    UserCouponMapper userCouponMapper;
    @Autowired
    InformationMapper informationMapper;
    @Autowired
    ParkingMapper parkingMapper;
    @Autowired
    ComplaintMapper complaintMapper;

    @Override
    public UserInfo selectOneUserInfo(Integer uiid) {
        return userInfoMapper.selectByPrimaryKey(uiid);
    }

    @Override
    public List<Comment> selectAllConmmont(Integer pid) {
        return null;
    }

    @Override
    public void updateUserInfo(UserInfo userInfo) {
        userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public void updateUser(User loginUser) {
        userMapper.updateByPrimaryKeySelective(loginUser);
    }

    @Override
    public List<Orders> selectOrders(Integer uiid, Integer page) {
        OrdersExample example = new OrdersExample();
        example.createCriteria().andUiidEqualTo(uiid).andStatusNotEqualTo("未出租");
        PageHelper.startPage(page, 10);
        List<Orders> ordersList = ordersMapper.selectByExample(example);
        return ordersList;
    }

    @Override
    public void uploadIdCard(String front, String back, Integer uiid) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(uiid);
        userInfo.setIdCardFront(front);
        userInfo.setIdCardBack(back);
        userInfoMapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public List<UserCoupon> selectAllCoupon(Integer uiid) {
        UserCouponExample example = new UserCouponExample();
        example.createCriteria().andUiidEqualTo(uiid).andUcstatusEqualTo("未使用");
        return userCouponMapper.selectByExample(example);
    }

    @Override
    public User selectOneUser(int uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public void uploadPlate(String plateimg, String driverimg, Integer uiid) {
        InformationExample example = new InformationExample();
        example.createCriteria().andUiidEqualTo(uiid);
        List<Information> temp = informationMapper.selectByExample(example);
        if (temp.size() == 0) {
            Information information = new Information();
            information.setPlateimg(plateimg);
            information.setDriverimg(driverimg);
            information.setUiid(uiid);
            informationMapper.insertSelective(information);
        }
    }

    @Override
    public void referralCode(UserInfo loginUserInfo) {
        userInfoMapper.updateByPrimaryKey(loginUserInfo);
    }

    @Override
    public Information selectOneInformation(Integer uiid) {
        InformationExample example = new InformationExample();
        example.createCriteria().andUiidEqualTo(uiid);
        List<Information> informationList = informationMapper.selectByExample(example);
        return informationList == null ? new Information() : informationList.get(0);
    }

    @Override
    public void refund(Integer pid2) {
        Orders orders = ordersMapper.selectByPrimaryKey(pid2);
        orders.setStatus("退款中");
        ordersMapper.updateByPrimaryKey(orders);
    }

    @Override
    public void recharge(String uiid, String money) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(Integer.parseInt(uiid));
        userInfo.setBalance(userInfo.getBalance().add(new BigDecimal(money)));
        userInfoMapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public void addComment(Comment comment) {
        comment.setStatus("待审核");
        commentMapper.insertSelective(comment);
    }

    @Override
    public void report(Complaint complaint, String pid) {
        Parking parking = parkingMapper.selectByPrimaryKey(Integer.parseInt(pid));
        System.out.println("parking = " + parking);
        if (parking != null) {
            complaint.setComuser(parking.getUiid());
            complaint.setTime(new Date());
            complaint.setStatus("待处理");
            System.out.println("complaint = " + complaint);
            complaintMapper.insertSelective(complaint);
        } else {
            return;
        }
    }

    @Override
    public void topup(Integer uiid, String combo) {

        if (combo.equals("99.00")) {
            for (int i = 0; i < 5; i++) {
                UserCoupon userCoupon = new UserCoupon();
                userCoupon.setCpid(1);
                userCoupon.setUiid(uiid);
                userCoupon.setUcstatus("未使用");
                userCouponMapper.insert(userCoupon);
            }

            UserCoupon userCoupon = new UserCoupon();
            userCoupon.setCpid(5);
            userCoupon.setUiid(uiid);
            userCoupon.setUcstatus("未使用");
            userCouponMapper.insert(userCoupon);

            for (int i = 0; i < 2; i++) {
                userCoupon.setCpid(2);
                userCoupon.setUiid(uiid);
                userCoupon.setUcstatus("未使用");
                userCouponMapper.insert(userCoupon);
            }
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(uiid);
            userInfo.setBalance(userInfo.getBalance().add(new BigDecimal(99)));
            userInfoMapper.updateByPrimaryKey(userInfo);

        } else if (combo.equals("299.00")) {
            for (int i = 0; i < 10; i++) {
                UserCoupon userCoupon = new UserCoupon();
                userCoupon.setUiid(uiid);
                userCoupon.setCpid(1);
                userCoupon.setUcstatus("未使用");
                userCouponMapper.insert(userCoupon);
            }
            for (int i = 0; i < 3; i++) {
                UserCoupon userCoupon = new UserCoupon();
                userCoupon.setCpid(5);
                userCoupon.setUiid(uiid);
                userCoupon.setUcstatus("未使用");
                userCouponMapper.insert(userCoupon);
            }

            for (int i = 0; i < 8; i++) {
                UserCoupon userCoupon = new UserCoupon();
                userCoupon.setCpid(2);
                userCoupon.setUiid(uiid);
                userCoupon.setUcstatus("未使用");
                userCouponMapper.insert(userCoupon);
            }
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(uiid);
            userInfo.setBalance(userInfo.getBalance().add(new BigDecimal(299)));
            userInfoMapper.updateByPrimaryKey(userInfo);
        }


    }

    @Override
    public void withdrawMoney(Integer uiid, String money) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(uiid);
        userInfo.setBalance(userInfo.getBalance().subtract(new BigDecimal(money)));
        userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

}
